package com.lcc.generate.clazz;

import com.alibaba.druid.util.StringUtils;
import com.lcc.generate.Config;
import com.lcc.generate.TableConfig;
import com.lcc.generate.util.DruidPool;
import com.lcc.generate.util.MyField;
import com.lcc.generate.util.DateUtil;
import com.lcc.generate.util.FileUtils;
import com.lcc.generate.util.JDBCUtil;

import java.io.File;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.*;

/**
 * 实体类生成工具
 *
 *   "classPath": "/java/com/lcc/lynx/base/system/pojo",               "//": "实体pojo文件相对根路径",
 *   "mapperXlmPath": "/resources/mapper",                             "//": "实体mapper.xml文件相对根路径",
 *   "mapperClassPath": "/java/com/lcc/lynx/base/system/mapper",       "//": "mapper接口文件相对根路径",
 *   "servicePath": "/java/com/lcc/lynx/base/system/service",          "//": "service接口文件相对根路径",
 *   "serviceImplPath": "/java/com/lcc/lynx/base/system/service",      "//": "service接口文件相对根路径",
 *   "controllerPath": "/java/com/lcc/lynx/base/system/controller",    "//": "controller类文件相对根路径",
 */
public class ClassUtils {
    
    // 文件路径
    public static String packageName      ;
    public static String classPath        ;
    public static String mapperXlmPath    ;
    public static String mapperClassPath  ;
    public static String servicePath      ;
    public static String serviceImplPath  ;
    public static String controllerPath   ;

    // 实体类包名
    private static String classPackage      ;
    private static String servicePackage    ;
    private static String controllerPackage ;
    public static String  mapperClassPackage;

    private static Date date;

    private static boolean generateXmlAnnotation = false;

    private static Config generateConfig;
    public static List<List<MyField>> generate(Config generateConfig) throws SQLException, ClassNotFoundException {
        ClassUtils.generateConfig = generateConfig;

        // 生成XML字段注解
//        generateXmlAnnotation = true;

        packageName      = generateConfig.packageName.replace(".","/")      ;
        classPath        = "/java/" + packageName + "/pojo"      ;
        mapperXlmPath    = "/resources/mapper"  ;
        mapperClassPath  = "/java/" + packageName + "/mapper";
        servicePath      = "/java/" + packageName + "/service"    ;
        serviceImplPath  = "/java/" + packageName + "/service"    ;
        controllerPath   = "/java/" + packageName + "/controller" ;
        classPackage = classPath.substring(classPath.indexOf("com/")).replace("/",".");
        servicePackage = servicePath.substring(servicePath.indexOf("com/")).replace("/",".");
        controllerPackage = controllerPath.substring(controllerPath.indexOf("com/")).replace("/",".");
        mapperClassPackage = mapperClassPath.substring(mapperClassPath.indexOf("com/")).replace("/",".");

        Connection con = DruidPool.getConnection();
        assert con != null;
        date = new Date();
        DatabaseMetaData metaData = con.getMetaData();

        List<List<MyField>> arr = new ArrayList<>();
        for (TableConfig config : generateConfig.generateConfigs) {
            Set<String> importSet = new HashSet<>();
            String nameHump = JDBCUtil.toHump(config.getTableName(generateConfig.ifNoPrefix), true);
            config.className = classPackage+"."+nameHump;

            // 获取主键
            ResultSet primaryKeys = metaData.getPrimaryKeys(generateConfig.dbName, generateConfig.schemaPattern, config.getTableName(false));
            if(primaryKeys.next() && StringUtils.isEmpty(config.getPrimaryKey())){
                config.primaryKey = primaryKeys.getString("COLUMN_NAME");
                if(primaryKeys.next()){
                    config.primaryKey = null;
                }
                primaryKeys.close();
            }
            boolean hasId = !StringUtils.isEmpty(config.primaryKey);

            // 获取表字段结构
            ResultSet columnResultSet = metaData.getColumns(generateConfig.dbName, generateConfig.schemaPattern, config.getTableName(false), "%");
            List<MyField> fieldList = new ArrayList<>();
            while (columnResultSet.next()) {
                // 字段名称
                String columnName = columnResultSet.getString("COLUMN_NAME");
                String name = JDBCUtil.ifToHump(generateConfig.ifAutoToHump, columnName, false);
                // 数据类型
                String columnType = columnResultSet.getString("TYPE_NAME");
                // 字段长度
                int datasize = columnResultSet.getInt("COLUMN_SIZE");
                // 小数部分位数
                int digits = columnResultSet.getInt("DECIMAL_DIGITS");
                // 描述
                String remarks = columnResultSet.getString("REMARKS");
                if(StringUtils.isEmpty(remarks)){
                    remarks = name;
                }
                if(StringUtils.isEmpty(config.tableComment)){
                    config.tableComment = remarks;
                }
                boolean isHasDefaultValue = !StringUtils.isEmpty(columnResultSet.getString("COLUMN_DEF"));
                boolean required = columnResultSet.getInt("NULLABLE") == 0;
                String defaultValue = columnResultSet.getString("COLUMN_DEF");
                String classType = getType(columnType, importSet, datasize, digits);
                MyField myField = new MyField(name, columnName, columnType, classType, remarks, required, defaultValue, datasize, digits, isHasDefaultValue);
                if(myField.searchType.contains("list") || (hasId && myField.column.equals(config.primaryKey) && !myField.column.equals("id")) ){
                    importSet.add("import java.util.List;\n");
                }
                if(myField.required && !myField.isHasDefaultValue && !myField.column.equals(config.primaryKey)){
                    importSet.add("import com.lcc.lynx.jdbc.annotation.InsertRequired;\n");
                }
                if(generateConfig.ifFieldLabel){
                    importSet.add("import com.lcc.lynx.jdbc.annotation.FieldLabel;\n");
                }
                fieldList.add(myField);
            }
            arr.add(fieldList);
            columnResultSet.close();
            StringBuilder classFileStr = new StringBuilder("package ").append(classPackage).append(";\n\n");
            // 生成Service和Controller
            generateClass(classFileStr, config, hasId, fieldList, importSet);
            generateServiceAndController(classFileStr, config);
        }
        return arr;
    }
    private static void generateClass(StringBuilder classFileStr, TableConfig config, boolean hasId,  List<MyField> fieldList, Set<String> importSet){
        String nameHump = JDBCUtil.toHump(config.getTableName(generateConfig.ifNoPrefix), true);
        File file = new File(generateConfig.rootPath + classPath);

        classFileStr.append(ifStr(generateConfig.ifLombok, "import lombok.Data;\n"))
                .append("import com.lcc.lynx.jdbc.pojo.TablePojo;\n")
                .append(ifStr(generateConfig.generateDefClass, "import com.lcc.lynx.jdbc.annotation.Table;\n"))
                .append(ifStr(hasId, "import com.lcc.lynx.jdbc.annotation.TableId;\n"))
                .append("${importSet}\n")
                .append(getComment(config.tableComment, "实体"))
                .append(ifStr(generateConfig.ifLombok, "@Data\n"))
                .append(ifStr(generateConfig.generateDefClass, "@Table(\"" + config.getTableName() + "\")\n"))
                .append("public class ").append(nameHump).append(" extends TablePojo {\n\n");

        if(generateConfig.generateXmlAnnotation){
            importSet.add("import com.thoughtworks.xstream.annotations.XStreamAlias;\n");
        }

        for (MyField myField : fieldList) {
            String fieldLabelValue = myField.label;
            classFileStr.append("\t/** ").append(ifStr(myField.remark)).append(" */\n")
                    .append(ifStr(hasId && myField.column.equals(config.primaryKey), "\t@TableId\n"))
                    .append(ifStr(myField.required && !myField.isHasDefaultValue && !myField.column.equals(config.primaryKey), "\t@InsertRequired\n"))
                    .append(ifStr(!StringUtils.isEmpty(fieldLabelValue) && generateConfig.ifFieldLabel, "\t@FieldLabel(\"" + fieldLabelValue + "\")\n"))
                    .append(ifStr(generateConfig.generateXmlAnnotation, "\t@XStreamAlias(\"" + myField.name.toUpperCase() + "\")\n"))
                    .append("\tprivate ").append(myField.classType).append(" ").append(myField.name).append(";\n\n");
        }

        for (MyField myField : fieldList) {
            String nameH = JDBCUtil.ifToHump(generateConfig.ifAutoToHump, myField.column, true);
            if(!generateConfig.ifLombok) {
                // get
                classFileStr.append("\tpublic ").append(myField.classType)
                        .append(" get").append(nameH)
                        .append("(){ return this.").append(myField.name)
                        .append("; }\n");
                // set
                classFileStr.append("\tpublic void set").append(nameH)
                        .append("(").append(myField.classType).append(" ").append(myField.name)
                        .append("){ this.").append(myField.name).append("=").append(myField.name)
                        .append("; }\n");
            }
            // 拓展条件
            if(myField.searchType.contains("like")){
                addPut(classFileStr, nameH, "Like", myField.name);
                addPut(classFileStr, nameH, "LikeL", myField.name);
            }
            if(myField.searchType.contains("time")){
                addPut(classFileStr, nameH, "Start", myField.name);
                addPut(classFileStr, nameH, "End", myField.name);
            }
            if(myField.searchType.contains("list") || (hasId && myField.column.equals(config.primaryKey)) && !myField.column.equals("id")){
                classFileStr.append("\tpublic void set").append(nameH).append("InList(List<").append(myField.classType).append("> ").append(myField.name)
                        .append("List){ put(\"").append(myField.name).append("InList\", ").append(myField.name).append("List)")
                        .append("; }\n");
            }
        }

        File file1 = new File(file, nameHump+".java");
        int i = classFileStr.indexOf("${importSet}");
        classFileStr.replace(i,i+12, String.join("", importSet));
        classFileStr.append("}");
        if(generateConfig.coverBaseClass || !file1.exists()){
            System.out.println("生成：" + file1.getName());
            FileUtils.write(file1, classFileStr.toString());
        }

        String excelName = nameHump + "Excel";

        // 添加DTO、VO
        classFileStr.delete(0, classFileStr.length());
        classFileStr.append("package ").append(classPackage).append(".dv;\n\n")
                .append("import ").append(config.className).append(";\n")
                .append(ifStr(generateConfig.generateExcelClass,"import com.lcc.lynx.excel.IExcel;\n"))
                .append(ifStr(generateConfig.generateExcelClass,"import " + classPackage + ".excel." + excelName + ";\n"))
                .append(ifStr(generateConfig.ifLombok,"import lombok.Data;\n")).append("\n")
                .append(getComment( config.tableComment, "参数实体"))
                .append(ifStr(generateConfig.ifLombok, "@Data\n"))
                .append("public class ").append(nameHump).append("DV extends ").append(nameHump).append(" ")
                .append(ifStr(generateConfig.generateExcelClass, "implements IExcel<" +excelName+"> " )).append("{\n")
                .append("\n\n}");
        File file2 = new File(file, "dv/"+nameHump+"DV.java");
        if(generateConfig.coverDVFile || !file2.exists()){
            System.out.println("生成：" + file2.getName());
            FileUtils.write(file2, classFileStr.toString());
        }

        // 添加Excel
        if(generateConfig.generateExcelClass){
            classFileStr.delete(0, classFileStr.length());
            classFileStr.append("package ").append(classPackage).append(".excel;\n\n")
                    .append("import com.lcc.lynx.excel.annotation.Excel;\n")
                    .append("import cn.idev.excel.annotation.ExcelProperty;\n")
                    .append("import cn.idev.excel.enums.poi.HorizontalAlignmentEnum;\n")
                    .append(ifStr(fieldList.stream().anyMatch(item -> item.isTime),
                            "import java.util.Date;\n" +
                            "import cn.idev.excel.annotation.format.DateTimeFormat;\n"))
                    .append("import cn.idev.excel.annotation.write.style.ColumnWidth;\n")
                    .append("import cn.idev.excel.annotation.write.style.ContentStyle;\n")
                    .append(ifStr(generateConfig.ifLombok,"import lombok.Data;\n")).append("\n\n")
                    .append(getComment( config.tableComment, "Excel实体"))
                    .append(ifStr(generateConfig.ifLombok, "@Data\n"))
                    .append("@ColumnWidth(12)\n")
                    .append("@ContentStyle(horizontalAlignment = HorizontalAlignmentEnum.CENTER)\n")
                    .append("public class ").append(excelName).append(" {\n");

            for (MyField myField : fieldList) {
                int width = 12;
                if(myField.isTime) width = 20;
                if(myField.classType.contains("Short")) width = 10;
                classFileStr.append("\t@ExcelProperty(\"").append(myField.remark).append("\")");
                if(width != 12 && myField.dictCode == null){
                    classFileStr.append("\n\t@ColumnWidth(").append(width).append(")");
                }
                if(myField.dictCode != null){
                    classFileStr.append("\n\t@Excel(dict = \"").append(myField.dictCode).append("\")");
                    classFileStr.append("\n\tprivate String ").append(myField.name).append(";\n\n");
                    continue;
                }
                if(myField.isTime){
                    classFileStr.append("\n\t@DateTimeFormat(\"yyyy-MM-dd hh:mm:ss\")");
                }
                classFileStr.append("\n\tprivate ").append(myField.classType).append(" ").append(myField.name).append(";\n\n");
            }
            classFileStr.append("}");
            File file3 = new File(file, "excel/"+nameHump+"Excel.java");
            if(generateConfig.coverExcelClass || !file3.exists()){
                System.out.println("生成：" + file3.getName());
                FileUtils.write(file3, classFileStr.toString());
            }
        }

        // 添加Def
        if(generateConfig.generateDefClass){
            String defName = config.getTableName().toUpperCase();
            classFileStr.delete(0, classFileStr.length());
            classFileStr.append("package ").append(classPackage).append(".def;\n\n")
                    .append("import com.lcc.lynx.jdbc.qw.Column;\n")
                    .append("import com.lcc.lynx.jdbc.qw.TableDef;\n")
                    .append(ifStr(fieldList.stream().anyMatch(item -> item.classType.contains("BigDecimal")), "import java.math.BigDecimal;\n"))
                    .append(ifStr(fieldList.stream().anyMatch(item -> item.classType.contains("BigInteger")), "import java.math.BigInteger;\n"))
                    .append("import ").append(classPackage).append(".dv.").append(nameHump).append("DV;\n\n")
                    .append(getComment( config.tableComment, "表定义"))
                    .append("public class ").append(defName).append(" extends TableDef {\n")
                    .append("\tprivate ").append(defName).append("() {super(").append(nameHump).append("DV.class);}\n")
                    .append("\tpublic final static ").append(defName).append(" _this_ = new ").append(defName).append("();\n\n")
                    .append("\tpublic final static Column<Object> all_columns = new Column<>(_this_, \"*\");\n");

            for (MyField myField : fieldList) {
                String type = myField.isTime ? "String" : myField.classType;
                classFileStr.append("\tpublic final static Column").append("<").append(type).append("> ").append(" ".repeat(Math.max(0, 6-type.length())))
                        .append(myField.column).append(" = new Column<>(_this_, \"").append(myField.column).append("\"")
                        .append(ifStr(myField.isTime, ", true")).append(");\n");
            }
            classFileStr.append("}");
            File file3 = new File(file, "def/"+defName+".java");
            System.out.println("生成：" + file3.getName());
            FileUtils.write(file3, classFileStr.toString());
        }
    }




    // 生成Service和Controller
    private static void generateServiceAndController(StringBuilder classFileStr, TableConfig config){
        String nameHump = JDBCUtil.toHump(config.getTableName(generateConfig.ifNoPrefix), true);
        String name = JDBCUtil.toHump(config.getTableName(generateConfig.ifNoPrefix), false);

        File sFile = new File(generateConfig.rootPath + servicePath);
        File siFile = new File(generateConfig.rootImplPath + serviceImplPath);
        File cFile = new File(generateConfig.rootControllerPath + controllerPath);
        File mFile = new File(generateConfig.rootMapperPath + mapperClassPath);


        boolean hasId = !StringUtils.isEmpty(config.primaryKey);
        String mapping = config.requestMapping;
        if(StringUtils.isEmpty(mapping)) mapping = "/" + config.getTableName(generateConfig.ifNoPrefix).replace("_", "/");
        config.requestMapping = mapping;
        // 生成mapper
        classFileStr.delete(0, classFileStr.length());
        classFileStr.append("package ").append(mapperClassPackage).append(";\n\n")
                .append("import ").append(classPackage).append(".dv.").append(nameHump).append("DV;\n")
                .append("import com.lcc.lynx.jdbc.IBaseMapper;\n")
                .append("import org.apache.ibatis.annotations.Mapper;\n\n")
                .append(getComment( config.tableComment, "Mapper"))
                .append("@Mapper\n")
                .append("public interface ").append(nameHump).append("Mapper extends IBaseMapper<").append(nameHump).append("DV>{\n")
                .append("\n\n}");
        File file = new File(mFile, nameHump+"Mapper.java");
        if(generateConfig.coverMainMapperClass || !file.exists()){
            System.out.println("生成：" + file.getName());
            FileUtils.write(file, classFileStr.toString());
        }

        // 添加Service
        classFileStr.delete(0, classFileStr.length());
        classFileStr.append("package ").append(servicePackage).append(";\n\n")
                .append("import ").append(classPackage).append(".dv.").append(nameHump).append("DV;\n")
                .append("import ").append(mapperClassPackage).append(".").append(nameHump).append("Mapper;\n")
                .append("import com.lcc.lynx.jdbc.").append(hasId?"IByIdService":"IBaseService").append(";\n\n")
                .append(getComment( config.tableComment, "服务"))
                .append("public interface ").append(nameHump).append("Service extends ").append(hasId?"IByIdService":"IBaseService").append("<").append(nameHump).append("DV>{\n\n")
                .append("\t").append(nameHump).append("Mapper getMapper();\n")
                .append("\n\n}");
        File file4 = new File(sFile, nameHump+"Service.java");
        if(generateConfig.coverMainServiceIntf || !file4.exists()){
            System.out.println("生成：" + file4.getName());
            FileUtils.write(file4, classFileStr.toString());
        }

        classFileStr.delete(0, classFileStr.length());
        classFileStr.append("package ").append(servicePackage).append(".impl;\n\n")
                .append("import ").append(servicePackage).append(".").append(nameHump).append("Service;\n")
                .append("import ").append(mapperClassPackage).append(".").append(nameHump).append("Mapper;\n")
                .append("import org.springframework.stereotype.Service;\n")
                .append(ifStr(generateConfig.ifLombok, "import lombok.AllArgsConstructor;\n"))
                .append(ifStr(!generateConfig.ifLombok, "import org.springframework.beans.factory.annotation.Autowired;\n"))
                .append("import org.springframework.transaction.annotation.Transactional;\n")
                .append(getComment( config.tableComment, "服务实现"))
                .append("@Service\n")
                .append("@Transactional\n")
                .append(ifStr(generateConfig.ifLombok, "import lombok.AllArgsConstructor;\n"))
                .append("public class ").append(nameHump).append("ServiceImpl")
                .append(" implements ").append(nameHump).append("Service {\n\n")
                .append(ifStr(!generateConfig.ifLombok, "    @Autowired\n"))
                .append("    private ").append(nameHump).append("Mapper mapper;\n")
                .append("    \n\n")
                .append("    @Override\n")
                .append("    public ").append(nameHump).append("Mapper getMapper() {\n")
                .append("       return mapper;\n")
                .append("    }\n")
                .append("\n}");
        File file5 = new File(siFile, "impl/" + nameHump+"ServiceImpl.java");
        if(generateConfig.generateService && (generateConfig.coverMainServiceImpl || !file5.exists())){
            System.out.println("生成：" + file5.getName());
            FileUtils.write(file5, classFileStr.toString());
        }

        // 添加Controller
        classFileStr.delete(0, classFileStr.length());
        classFileStr.append("package ").append(controllerPackage).append(";\n\n")
                .append("import ").append(servicePackage).append(".").append(nameHump).append("Service;\n")
                .append("import ").append(classPackage).append(".dv.").append(nameHump).append("DV;\n")
                .append("import com.lcc.lynx.jdbc.").append(hasId?"IByIdController":"IBaseController").append(";\n")
                .append(ifStr(generateConfig.ifLombok, "import lombok.AllArgsConstructor;\n"))
                .append(ifStr(!generateConfig.ifLombok, "import org.springframework.beans.factory.annotation.Autowired;\n"))
                .append(ifStr("mskc".equals(generateConfig.model) && !StringUtils.isEmpty(config.generateVue3),
                        "import org.springframework.stereotype.Controller;\n" +
                                ifStr("jsp".equals(config.generateVue3),
                                        "import com.github.pagehelper.PageInfo;\n" +
                                        "import com.lcc.lynx.jdbc.pojo.PageParams;\n" +
                                        "import com.zbiti.base.controller.support.ResponseObject;\n" +
                                        "import com.zbiti.base.mybatis.support.PageBean;\n") +
                        "import org.springframework.web.bind.annotation.PathVariable;\n" +
                        "import javax.servlet.http.HttpServletRequest;\n"))
                .append("import org.springframework.web.bind.annotation.RestController;\n")
                .append("import org.springframework.web.bind.annotation.RequestMapping;\n\n\n")
                .append(getComment(config.tableComment, "控制实现"))
                .append("mskc".equals(generateConfig.model) && !StringUtils.isEmpty(config.generateVue3) ? "@Controller\n" : "@RestController\n")
                .append(ifStr(generateConfig.ifLombok, "import lombok.AllArgsConstructor;\n"))
                .append("@RequestMapping(\"").append(mapping).append("\")\n")
                .append("public class ").append(nameHump).append("Controller")
                .append(" implements ").append(hasId?"IByIdController<":"IBaseController<").append(nameHump).append("DV>").append(" {\n\n")
                .append(ifStr(!generateConfig.ifLombok, "    @Autowired\n"))
                .append("    private ").append(nameHump).append("Service service;\n")
                .append("    \n\n")
                .append("    @Override\n")
                .append("    public ").append(nameHump).append("Service getService() {\n")
                .append("       return service;\n")
                .append("    }\n");
        if("jsp".equals(config.generateVue3)){
            classFileStr.append(
                    """
                        
                        @RequestMapping("/index")
                        public String index(${nameHump}DV params, HttpServletRequest request) {
                            PageInfo<${nameHump}DV> page = this.getService().selectPage(params, PageBean.getFromRequest(request));
                            ResponseObject ro = new ResponseObject();
                            ro.setPage(page);
                            request.setAttribute("ro", ro);
                            request.setAttribute("params", params);
                            return "base/${name}/index";
                        }
                    
                        @RequestMapping("/add")
                        public String add() { return "base/${name}/add"; }
                    
                        @RequestMapping("/edit/{id}")
                        public String edit(@PathVariable String id, HttpServletRequest request) {
                            request.setAttribute("entity", this.getService().selectById(id));
                            return "base/${name}/edit";
                        }
                    """.replace("${nameHump}", nameHump)
                            .replace("${name}", name)
            );
        }
        if("jsp2".equals(config.generateVue3)){
            classFileStr.append(
                    """
                        
                        @RequestMapping("/index")
                        public String index() {
                            return "base/${name}/index";
                        }
                    
                        @RequestMapping("/add")
                        public String add() { return "base/${name}/add"; }
                    
                        @RequestMapping("/edit/{id}")
                        public String edit(@PathVariable String id, HttpServletRequest request) {
                            request.setAttribute("entity", this.getService().selectById(id));
                            return "base/${name}/edit";
                        }
                    """.replace("${nameHump}", nameHump)
                            .replace("${name}", name)
            );
        }
        classFileStr.append("\n}");
        File file6 = new File(cFile, nameHump+"Controller.java");
        if(generateConfig.generateController &&  (generateConfig.coverMainController || !file6.exists())){
            System.out.println("生成：" + file6.getName());
            System.out.println();
            FileUtils.write(file6, classFileStr.toString());
        }

    }

    private static String getType(String columnType, Set<String> importSet, int datasize, int digits) {
        if(columnType.equals("numeric") || columnType.equals("NUMERIC")){
            columnType = "decimal";
        }
        String type = columnType.toLowerCase()+",";
        String classType;
        if("varchar,bpchar,char,text,blob,longtext,".contains(type)){
            classType = "String";
        }else if("int,int4,integer,".contains(type)){
            classType = "Integer";
        }else if("double,".contains(type)){
            classType = "Double";
        }else if("smallint,tinyint,int2,".contains(type) || "decimal,".equals(type) && datasize <= 2 && digits == 0){
            classType = "Short";
        }else if("bigint,bigserial,int8,".contains(type) || "decimal,".equals(type) && datasize <= 18 && digits == 0){
            classType = "Long";
        }else if("timestamp,date,datetime,".contains(type)){
            classType = "Date";importSet.add("import java.util.Date;\n");
        }else if("decimal,".equals(type) && digits == 0){
            classType = "BigInteger";importSet.add("import java.math.BigInteger;\n");
        }else if("decimal,".equals(type)){
            classType = "BigDecimal";importSet.add("import java.math.BigDecimal;\n");
        }else if("bool,tinyint,bit,".contains(type)){
            classType = "Boolean";
        }else{
            throw new RuntimeException("未匹配的数据库类型："+columnType);
        }
        return classType;
    }

    private static StringBuilder getComment(String nameHump, String desc){
         return new StringBuilder().append("/** \n * ").append(nameHump).append(desc).append("\n")
                .append(" * @auth ").append(generateConfig.author).append("\n")
                .append(" * @date ").append(DateUtil.getCompact_DF(date)).append("\n")
                .append(" */\n");
    }

    private static void addPut(StringBuilder classFileStr, String nameH, String suffix, String name){
//        classFileStr.append("\tpublic String get").append(nameH).append(suffix).append("(){")
//                .append(" return (String) get(\"").append(name).append(suffix).append("\");")
//                .append(" }\n");
        classFileStr.append("\tpublic void set").append(nameH).append(suffix).append("(String ").append(name).append(suffix)
                .append("){ put(\"").append(name).append(suffix).append("\", ").append(name).append(suffix).append(")")
                .append("; }\n");
    }

    public static String ifStr(boolean b, String str){
        return !b || str==null ?"":str;
    }
    public static String ifStr(String str){
        return str==null ?"":str;
    }
}
